package com.sen.demo;

import com.sen.demo.thread.CustomerRunable;
import com.sen.demo.thread.TradeRunable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @className: demoInit
 * @Auther: jiesen.liu
 * @Date: 2019/7/17 17:49
 * @package: com.sen.demo
 */
/*@Configuration*/
@Slf4j
public class demoInit implements InitializingBean {

    @Override
    public void afterPropertiesSet(){
        log.info("初始化项目-----------------");
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.initialize();
        scheduler.setPoolSize(3);
        scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        ReentrantLock lock = new ReentrantLock();
        scheduler.schedule(new CustomerRunable("线程1------",lock), new CronTrigger("0/1 * * * * ? "));
        scheduler.schedule(new CustomerRunable("线程2------",lock), new CronTrigger("0/1 * * * * ? "));
        scheduler.schedule(new CustomerRunable("线程3------",lock), new CronTrigger("0/1 * * * * ? "));
        scheduler.schedule(new CustomerRunable("线程4------",lock), new CronTrigger("0/3 * * * * ? "));

        //scheduler.schedule(new TradeRunable("线程5------"), new CronTrigger("0/3 * * * * ? "));
        //任务触发，可修改任务的执行周期
        /*ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.initialize();
        CronTrigger trigger = new CronTrigger("0/3 * * * * ? ");
        taskScheduler.schedule(new Runnable() {
            int i = 1;
            @Override
            public void run() {
                log.info("执行开始啦，{}",i);
                i++;
                if (i== 5) {
                    try {
                        Thread.sleep(10000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, trigger);*/
    }
}
